package com.comleader.uap.service;

import static cn.hutool.core.date.DateUtil.formatDateTime;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.comleader.uap.common.response.Result;
import com.comleader.uap.common.util.ExcelUtil;
import com.comleader.uap.common.util.ResponseUtil;
import com.comleader.uap.mapper.InternetSmsMapper;
import com.comleader.uap.pojo.dto.FiveEffectStatDTO;
import com.comleader.uap.pojo.dto.OverrallIndicatorDTO;
import com.comleader.uap.pojo.entity.InternetSms;
import com.comleader.uap.pojo.query.FiveEffectQuery;
import com.comleader.uap.pojo.query.InternetSmsQuery;
import com.comleader.uap.pojo.vo.InternetSmsDetailVO;
import com.comleader.uap.pojo.vo.InternetSmsListVO;
import com.comleader.uap.pojo.vo.ProtectUndoStatusVO;

import cn.hutool.core.collection.CollUtil;

/**
 * @author lianxinjing
 * @description 针对表【api_internet_sms(上网和短信业务保护撤销表)】的数据库操作Service
 * @createDate 2024-11-23 16:03:24
 */
@DS("mysql")
@Service
public class InternetSmsService extends ServiceImpl<InternetSmsMapper, InternetSms> {

    @Resource
    private CalledProtectLogInfoService calledProtectLogInfoService;

    public List<InternetSmsListVO> selectList(InternetSmsQuery query) {
        if (CollUtil.isNotEmpty(query.getIssueTime())) {
            query.setIssueStartTime(query.getIssueTime().get(0));
            query.setIssueEndTime(query.getIssueTime().get(1));
        }
        List<InternetSmsListVO> list = baseMapper.selectList(query);
        return list;
    }

    public InternetSmsDetailVO selectDetail(String calledNumber, String commandId) {
        List<InternetSmsDetailVO.PushAudit> pushAuditList = baseMapper.selectListByCommandIdAndNumber(commandId, calledNumber);
        List<ProtectUndoStatusVO> protectUndoStatusVOList = calledProtectLogInfoService.selectProtectUndoStatusList(commandId, calledNumber);
        return new InternetSmsDetailVO(pushAuditList, protectUndoStatusVOList);

    }

    public void export(InternetSmsQuery query, HttpServletResponse response) throws IOException {
        List<Date> issueTime = query.getIssueTime();
        if (CollUtil.isEmpty(issueTime) || issueTime.size() != 2) {
            ResponseUtil.write(response, Result.fail("必须选择开始时间和结束时间"));
            return;
        }
        try {
            String fileName = "上网/短信保护_" + formatDateTime(issueTime.get(0)) + "_" + formatDateTime(issueTime.get(1)) + ".xlsx";
            List<InternetSmsListVO> list = selectList(query);
            ExcelUtil.exportMultipleSheet(response, fileName, InternetSmsListVO.class, "上网短信保护", list);
        } catch (Exception e) {
            log.error("上网/短信保护导出异常", e);
            ResponseUtil.write(response, Result.fail("系统异常"));
        }

    }

    /**
     * @Description 统计上网/短信保护下发量、撤销量
     * @Auhtor 连新景
     * @Date 2024/12/03 16:56
     * @param query
     * @return com.comleader.uap.pojo.dto.OverrallIndicatorDTO.InternetSms
     */
    public OverrallIndicatorDTO.InternetSms countByProtectTypeAndDataType(FiveEffectQuery query) {
        return baseMapper.countByProtectTypeAndDataType(query);
    }

    public List<FiveEffectStatDTO.InternetSms> countByDimension(FiveEffectQuery query) {
        return baseMapper.countByDimension(query);
    }

}
